//
//  AppVersion.swift
//  AreaChainblock
//
//  Created by 朱杰 on 2018/12/1.
//  Copyright © 2018 zhaotongyue. All rights reserved.
//

import UIKit

class AppVersion: NSObject {
    static func checkAndToUpdata(){
        let infoDictionary = Bundle.main.infoDictionary
        let appVersion = infoDictionary!["CFBundleShortVersionString"] as! String
        MyNetwork.request(target: .checkAppVersion(device: "iOS"), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            let updateURL = ((result as AnyObject)["data"]as AnyObject)["updateURL"] as! String
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            if needModel.code == 1111{
                let webVersion = (((result as AnyObject)["data"]as AnyObject)["version"] as! String)
                let compare = appVersion.caseInsensitiveCompare(webVersion)
                switch compare.rawValue{
                case -1 :
                    print("app版本号<服务器版本号---需要更新")
                    let alertController = UIAlertController(title: XALanguageManager.getString(key: "版本更新"), message: XALanguageManager.getString(key: "前往版本更新"), preferredStyle: .alert);
                    let cance = UIAlertAction(title: "确定", style: .default) { (_) in
                        let url = URL(string: updateURL)!
                        if UIApplication.shared.canOpenURL(url as URL){
                            UIApplication.shared.open(url as URL, options: [:], completionHandler: { (istrue) in
                                
                            })
                        }
                    }
                    alertController.addAction(cance)
                    let rootVC = UIApplication.shared.delegate as! AppDelegate
                    rootVC.window?.rootViewController?.present(alertController, animated: true, completion: {})
                case 0:
                    print("app版本号=服务器版本号---不需更新")
                case 1:
                    print("app版本号>服务器版本号---后台bug")
                default:break
                }
            }
        }) { (error) in
            
        }
        
    }
}
